package littlewhisker.components.ui.dial {
	import flash.events.MouseEvent;
	import flash.display.Sprite;

	/**
	 * @author Philip
	 */
	public class RotarySelector extends AbstractDial {
		
		// The fixed positions must have incrementing order;
		// TODO: Replace with Vector.<Number> when FDT can handle it
		private var _fixedPositions:Array;
		
		private var _steps:uint;
		private var _step:uint = 0;
		
			
		
		public function get step() : uint {
			return _step;
		}
		
		public function set step(step : uint) : void {
			_step = step;
		}	
			
		public function get steps() : uint {
			return _steps;
		}
		
		public function set steps(steps : uint) : void {
			_steps = steps;
		}
				
		
		
		public function get fixedPositions() : Array {
			return _fixedPositions;
		}
		
		public function set fixedPositions(fixedPositions : Array) : void {
			_fixedPositions = fixedPositions;
			steps = fixedPositions.length;
		}
		
		private var ableToMove : Boolean = true;
		
		public function RotarySelector(asset:Sprite = null):void {
			if(asset){
				this.asset = asset;
			}
		}
		
		override protected function onMove(e:MouseEvent) : void {
			if(ableToMove){
				ableToMove = false;	
				var newX:Number = asset.stage.mouseX - prevX;
				var newY:Number =  prevY-asset.stage.mouseY;
				var distance:Number = (newX+newY)/2;
				if(distance > 0 && step<steps){
					step++;
					ableToMove = false;
				}
				if(distance < 0 && step > 0) {
					step--;
					ableToMove = false;
				}
			
				fixedPositions ? destination = fixedPositions[step] : getDestination(); destination = fixedPositions[step];//((180/6)*(Variables.CURRENT_SOUND))-(0.55*Variables.CURRENT_SOUND);
				rotate();
			}
		}
		
		/* TODO: calculate destination taking limitations into account */
		private function getDestination():Number{
			return (360/steps)*step;
		}
		
		
		override protected function doneMove():void{
			ableToMove = true;	
		}
		
	}
		
}
